define(['jquery', 'bootstrap', 'backend', 'form','upyun','summernote'], function ($, undefined, Backend, Form,UpYun,Summernote) {

    var Controller = {
        index: function () {
            var table = $("#table");
            $(document).on('click', '.thisaddtabs', function (e) {
                Backend.api.addtabs(Backend.api.replaceids(this, $(this).attr('url')), $(this).attr("title"));
                return false;
            });
            $(document).on('click', '#chkSelectAll', function (e) {
                var c = $(this).attr("checked");
                console.log(c);
                if(typeof(c)=='undefined'){
                    $(this).attr("checked","");
                }else{
                    $(this).removeAttr("checked");
                }
                $("#table input[name=btSelectItem]").each(function(){
                    $(this).prop("checked",!$(this).prop("checked"));
                });
                if($("input[name=btSelectItem]:checked").length>0){
                    $(".btn-del").removeClass('disabled');
                }else{
                    $(".btn-del").addClass('disabled');
                }
                return false;
            });
            $(document).on('click', 'input[name=btSelectItem]',function (e) {
                var ll = $("input[name=btSelectItem]:checked").length;
                if(ll > 0){
                    if(ll == 1){
                        $(".btn-edit").removeClass('disabled');
                    }else{
                        $(".btn-edit").addClass('disabled');
                    }
                    $(".btn-del").removeClass('disabled');
                    if(typeof($(".btn-more"))!='undefined'){
                        $(".btn-more").removeClass('disabled');
                    }
                }else{
                    $(".btn-edit").addClass('disabled');
                    $(".btn-del").addClass('disabled');
                    if(typeof($(".btn-more"))!='undefined'){
                        $(".btn-more").addClass('disabled');
                    }
                }
            });
            $(".btn-edit").on("click",function(e){
                e.preventDefault();
                var chenked = $("input[name='btSelectItem']:checked");
                Backend.api.addtabs(Backend.api.replaceids(this, 'category/edit?ids='+chenked[0].value),'编辑');
                return false;
            });
            $(document).on('click','.btn-del',function (e) {
                e.preventDefault();

                var chenked = $("input[name='btSelectItem']:checked");
                //alert(chenked);
                var ids = "";
                for(var i=0;i<chenked.length;i++){
                    ids += chenked[i].value+",";
                }
                //console.log(ids);
                if(chenked.length==0){
                    var index2 = Layer.confirm('请选择类别','请选择');
                }else{
                    var id = $(this).data("id");
                    var that = this;
                    var index = Layer.confirm(
                        __('Are you sure you want to delete this item?'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function () {
                            $.ajax({
                                type: "post",
                                url: "/cms.php/category/dels.html",
                                data: {ids:ids},
                                dataType: 'JSON',
                                success: function (data, status) {
                                    if (status == "success") {
                                        Layer.close(index);
                                        location.reload();
                                    }
                                },
                                error: function () {
                                    //alert('编辑失败');
                                },
                                complete: function () {

                                }
                            });
                        }
                    );
                }

            });
            $(".btn-delone").on("click",function(e){
                e.preventDefault();

                var ids = $(this).attr("title");

                var index = Layer.confirm(
                    __('Are you sure you want to delete this item?'),
                    {icon: 3, title: __('Warning'), shadeClose: true},
                    function () {
                        $.ajax({
                            type: "post",
                            url: "/cms.php/category/del.html",
                            data: {ids:ids},
                            dataType: 'JSON',
                            success: function (data, status) {
                                if (status == "success") {
                                    Layer.close(index);
                                    location.reload();
                                }
                            },
                            error: function () {
                                //alert('编辑失败');
                            },
                            complete: function () {

                            }
                        });
                    }
                );
            });
            $(".img-center").on("click",function(e){
                e.preventDefault();
                var data = [];
                var imgurl = $(this).attr("urls");
                imgurls = imgurl.split(",");
                $.each(imgurls, function (index, v) {
                    data.push({
                        src: v,
                    });
                });
                Layer.photos({
                    photos: {
                        "start": 0,
                        "data": data
                    },
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            });

            //表格切换
            /*
            //窗体太窄，自动缩小。
            if($(window).width()<768){
                $("#toggleview").trigger('click');
            }
            $(window).resize(function(){
                if($(window).width()<=768){
                    if(!$("#table thead").is(":hidden")){
                      $("#toggleview").trigger('click');
                    }
                }
                if($(window).width()>768){
                    if($("#table thead").is(":hidden")){
                      $("#toggleview").trigger('click');
                    }
                }
            });
            */
        },
        add: function () {
            $(document).on("change", "#c-moduleid", function (e) {
                $.ajax({
                    type: "post",
                    url: "/cms.php/category/ajaxlist",
                    data: {
                        moduleid: $(this).val()
                    },
                    dataType: 'JSON',
                    success: function (ret) {
                        var str = '';
                        str += '<option value="">请选择</option>';
                        $.each(eval(ret), function (k, v) {
                            str += '<option value="' + v.itemid + '"';
                            if (v.selected == 1) { str += ' selected="selected">'; }
                            else { str += '>'; }
                            str += v.name;
                            str += '</option>';
                        });
                        $("#c-pid").html(str);
                        $("#c-pid").selectpicker('refresh');
                    },
                    error: function () { alert('编辑失败'); },
                    complete: function () { }
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-moduleid", function (e) {
                $.ajax({
                    type: "post",
                    url: "/cms.php/category/ajaxlist",
                    data: {
                        moduleid: $(this).val()
                    },
                    dataType: 'JSON',
                    success: function (ret) {
                        var str = '';
                        str += '<option value="">请选择</option>';
                        $.each(eval(ret), function (k, v) {
                            str += '<option value="' + v.itemid + '"';
                            if (v.selected == 1) { str += ' selected="selected">'; }
                            else { str += '>'; }
                            str += v.name;
                            str += '</option>';
                        });
                        $("#c-pid").html(str);
                        $("#c-pid").selectpicker('refresh');
                    },
                    error: function () { alert('编辑失败'); },
                    complete: function () { }
                });
            });

            
            $(".returnback").click(function(){
                history.back();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
